import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_boss/page/home/home.dart';

class IndexPage extends StatefulWidget {
  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {
  final TextEditingController searchTextEditingController =
      TextEditingController();
  List data = List();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        primary: true,
        children: <Widget>[
          _head(context),
          _body(context),
        ],
      ),
    );
  }

  //头部
  Widget _head(BuildContext context) {
    return Container(
      height: 196.0,
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage("images/home-bg.png"),
          fit: BoxFit.fill,
        ),
      ),
      child: Stack(
        children: <Widget>[
          Positioned(
            top: 30.0,
            width: MediaQuery.of(context).size.width,
            child: Column(
              children: <Widget>[
                Text(
                  "找工作要跟老板谈",
                  style: TextStyle(fontSize: 30.0, color: Colors.white),
                ),
                Text(
                  "BOSS直聘",
                  style: TextStyle(fontSize: 30.0, color: Colors.white),
                ),
              ],
            ),
          ),
          Positioned(
            top: 138.0,
            left: 15.0,
            right: 15.0,
            height: 37.0,
            child: TextField(
//                    autofocus: true,//是否获取焦点
              controller: searchTextEditingController,
              decoration: InputDecoration(
                contentPadding: EdgeInsets.only(left: 20.0),
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.all(
                    Radius.circular(30.0),
                  ),
                ),
                hintText: "搜索职位/公司",
                hintStyle: TextStyle(
                  color: Color.fromRGBO(174, 174, 174, 1.0),
                ),
                fillColor: Colors.white,
                filled: true,
                suffixIcon: FlatButton(
                  onPressed: () {
                    Navigator.of(context).push(MaterialPageRoute(
                        builder: (BuildContext context) => HomePage()));
                  },
                  child: Text(
                    "搜索",
                    style: TextStyle(
                      color: Color.fromRGBO(91, 212, 199, 1.0),
                    ),
                  ),
                ),
              ),
              onSubmitted: (text) {},
            ),
          )
        ],
      ),
    );
  }

  //身体
  _body(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(left: 15.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: data.length > 0 ? _getClassifyList(context) : [],
      ),
    );
  }

  List<Widget> _getClassifyList(BuildContext context) {
    List<Widget> classifyList = List();
    for (int i = 0; i < data.length; i++) {
      classifyList.add(_classify(context, data[i]));
    }
    return classifyList;
  }

  //分类
  _classify(BuildContext context, dynamic classify) {
    return Padding(
      padding: EdgeInsets.only(top: 18.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Row(
            children: <Widget>[
              Image.asset(
                '${classify["classifyImage"]}',
                width: 50.0,
                height: 50.0,
              ),
              Padding(
                padding: EdgeInsets.only(
                  top: 14.0,
                  left: 10.0,
                ),
                child: Text(
                  '${classify["classifyName"]}',
                  style: TextStyle(
                    fontSize: 15.0,
                    fontWeight: FontWeight.w700,
                  ),
                ),
              ),
            ],
          ),
          Wrap(
            children: _getLabelList(context, classify["labelList"]),
          )
        ],
      ),
    );
  }

  _getLabelList(BuildContext context, List labelList) {
    List<Widget> labels = List();
    for (int i = 0; i < labelList.length; i++) {
      labels.add(_label(context, labelList[i]["label"]));
    }
    return labels;
  }

  //标签组件封装
  _label(BuildContext context, String label) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.black12,
        borderRadius: BorderRadius.circular(100.0),
      ),
      padding: EdgeInsets.only(
        top: 4.0,
        bottom: 4.0,
        left: 8.0,
        right: 8.0,
      ),
      margin: EdgeInsets.only(top: 5.0, right: 10.0),
      child: Text(
        "$label",
        style: TextStyle(
          fontSize: 13.0,
        ),
      ),
    );
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    //初始化
    _init();
  }

  _init() async {
    String json =
        await DefaultAssetBundle.of(context).loadString("json/index.json");
    setState(() {
      data.addAll(jsonDecode(json));
    });
  }
}
